<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Queries\WlGoodsSingleQuery;
use App\Http\Requests\Api\WatchLogRequest;
use App\Http\Requests\Api\WlGoodsRequest;
use App\Http\Resources\WlGoodsResource;
use App\Models\WatchLog;
use Carbon\Carbon;
class WlGoodsController extends Controller
{
    public function show($goodsId, WlGoodsSingleQuery $query)
    {
        $goods = $query->findOrFail($goodsId);
        return new WlGoodsResource($goods);
    }

    public function store_watch_log(WatchLogRequest $request, WatchLog $model)
    {
        $log_count = $model->where("user_id", $request->user()->id)
            ->where("goods_id", $request->goods_id)
            ->where("user_phone", $request->user()->phone)
            ->count();
        if ($log_count === 0) {
            $data = [
                "watch_time" => time(),
                "user_phone" => $request->user()->phone,
                "user_id" => $request->user()->id,
                "goods_id" => $request->goods_id,
                "location_id" => $request->location_id,
            ];
            WatchLog::create($data);
        }
        //不是今天注册的
        if (Carbon::now()->toDateString() != $request->user()->created_at->toDateString()) {
            $status = 201;
        }else{
            $status = 200;
        }
        $request->user()->new_login_time = Carbon::now()->toDateTimeString();
        $request->user()->save();
        return response(null, $status);
    }
}
